package com.example.crazyfall;

import java.io.IOException;

import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
///import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.BaseGameActivity;

import android.view.KeyEvent;

//import java.io.IOException;
//
//import org.andengine.engine.Engine;
//import org.andengine.engine.LimitedFPSEngine;
//import org.andengine.engine.camera.Camera;
//import org.andengine.engine.options.EngineOptions;
//import org.andengine.engine.options.ScreenOrientation;
////import org.andengine.engine.options.WakeLockOptions;
//import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
//import org.andengine.entity.scene.Scene;
//import org.andengine.ui.activity.SimpleBaseGameActivity;


public class MainActivity extends BaseGameActivity  {
	
	// Dimensiones de la pantalla 
	 private static final int ANCHO_CAMARA = 520; 
	 private static final int ALTO_CAMARA = 855;
	 private Camera camara;
	 @SuppressWarnings("unused")
	private ResorucesManager resourcesManager;
	 
	 // Constructor del engine
@Override
public EngineOptions onCreateEngineOptions() {
	// TODO Auto-generated method stub
	this.camara = new Camera(0, 0, ANCHO_CAMARA, ALTO_CAMARA); 
	EngineOptions opciones = new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(ANCHO_CAMARA,ALTO_CAMARA), this.camara);
	//Audio como todavia no se implementa lo dejamos en false
	opciones.getAudioOptions().setNeedsMusic(false).setNeedsSound(false);
	return opciones;
}

	//Metodo para que el engine corra en diferentes dispositivos
public Engine onCreateEngine(EngineOptions pEngineOptions){
	return new LimitedFPSEngine(pEngineOptions, 60);
}

	//Metodo para crear los recursos
@Override
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback)
		throws IOException {
	// TODO Auto-generated method stub
	ResorucesManager.prepareManager(mEngine, this, camara, getVertexBufferObjectManager());
	resourcesManager = ResorucesManager.getInstance();
	pOnCreateResourcesCallback.onCreateResourcesFinished();
	
}

	//Metodo para crear la escena splash de arranque
@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)	throws IOException {
	// TODO Auto-generated method stub
	SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
	
}

	//Metodo para crear la escena base del menu y cargar mientras el splash
@Override
public void onPopulateScene(Scene pScene,OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException {
	// TODO Auto-generated method stub
	  mEngine.registerUpdateHandler(new TimerHandler(2f, new ITimerCallback() 
	    {
	        public void onTimePassed(final TimerHandler pTimerHandler) 
	        {
	            mEngine.unregisterUpdateHandler(pTimerHandler);
	            SceneManager.getInstance().createMenuScene();
	        }
	    }));
  pOnPopulateSceneCallback.onPopulateSceneFinished();

}

	//Metodo para destruir  cualquier escena o ciclo que este activo y salir del sistema 
@Override
protected void onDestroy(){
	super.onDestroy();
	System.exit(0);
}

	//Metodo que valida que en realidad se haya oprimido el boton de regreso
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
	if(keyCode == KeyEvent.KEYCODE_BACK){
		SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
	}
	return false;
	
}

	
}
